package com.guang.springbootrequestversion.controller;

import com.guang.springbootrequestversion.bean.Cat;
import com.guang.springbootrequestversion.bean.Person;
import com.guang.springbootrequestversion.resolver.UserParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 参数映射原理
 * @author liguang
 * @date 2022/5/11 16:02
 */
@Controller
public class TestParamerControllerOne {

    private static final Logger logger = LoggerFactory.getLogger(TestParamerControllerOne.class);

    @ResponseBody
    @PostMapping(path = "saveuser")
    public Person person(Person person){
        logger.info("对应的请求参数是：{}",person);
        return person;
    }

    @GetMapping(path = "test")
    public void wan(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
        httpServletRequest.setAttribute("name","tom");
        httpServletRequest.setAttribute("age",20);
        httpServletRequest.getRequestDispatcher("test1").forward(httpServletRequest,httpServletResponse);
    }

    @ResponseBody
    @GetMapping(path = "test1")
    public Cat selfParameter(@UserParam Cat cat){
        // 每次打印参数过于麻烦，写个切面来打印所有的参数
        logger.info("对应的请求参数是：{}",cat);
        return cat;
    }

}
